﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditPluginTemplate : AdminPage, IEditPluginTemplateView
    {
        private EditPluginTemplatePresenter _presenter;


        public override string HelpPage
        {
            get { return "dialogs/edit-plugin-template.aspx"; }
        }

        #region IEditPluginTemplateView Members

        public int TemplateID
        {
            get
            {
                if (ViewState["TemplateID"] == null)
                    ViewState["TemplateID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["TemplateID"];
            }
        }


        public void SetTemplateID(int id)
        {
            ViewState["TemplateID"] = id;
        }


        public int PluginID
        {
            get
            {
                if (ViewState["PluginID"] == null)
                    ViewState["PluginID"] = QueryString.GetInt32ID("pluginid");
                return (int) ViewState["PluginID"];
            }
            set { ViewState["PluginID"] = value; }
        }


        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }


        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }


        public string SourceCode
        {
            get { return hidCode.Value; }
            set { hidCode.Value = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditPluginTemplate.xml"; }
        }


        public IList<PluginTemplateLiteral> GetPluginLiterals(string templateVirtualPath)
        {
            Control template = Page.LoadControl(templateVirtualPath);
            IList localizedLiteralControls = ControlFinder.FindControls(typeof(LocalizedPluginLiteral), template);
            IList<PluginTemplateLiteral> literals = new List<PluginTemplateLiteral>();
            foreach (LocalizedPluginLiteral literalControl in localizedLiteralControls)
            {
                var literal = new PluginTemplateLiteral();
                literal.ControlID = literalControl.ID;
                literals.Add(literal);
            }
            return literals;
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditPluginTemplatePresenter(this);
            if (TemplateID > 0)
                HeadingText = GetText(this, "EditHeading");
            else
                HeadingText = GetText(this, "NewHeading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            hlCancel.NavigateUrl = "Admin/PluginTemplates.aspx?id=" + PluginID;
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            RegisterUpdateLiteralsCall(false);
        }


        protected void lbSaveAndReturn_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            RegisterUpdateLiteralsCall(true);
        }


        private void RegisterUpdateLiteralsCall(bool redirect)
        {
            if (_presenter.IsValid)
            {
                if (redirect)
                    btnUpdateLiterals.CommandArgument = "redirect";
                else
                    btnUpdateLiterals.CommandArgument = "";
                ClientScriptProxy.Current.RegisterStartupScript(this, GetType(), "updateLiterals",
                                                                "document.getElementById('" + btnUpdateLiterals.ClientID +
                                                                "').click();", true);
            }
        }


        protected void btnUpdateLiterals_Click(object sender, EventArgs e)
        {
            _presenter.UpdateLiterals();
            if (_presenter.IsValid && btnUpdateLiterals.CommandArgument == "redirect")
                Response.Redirect("PluginTemplates.aspx?id=" + PluginID);
        }
    }
}